﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
using KPIS.GERP.GBMS.IBLL;
using KPIS.GERP.GBMS.BLL;
using KPIS.GERP.GBMS.MODEL;
using KPIS.GERP.GBMS.APP_UI.Common;

namespace KPIS.GERP.GBMS.APP_UI.Form
{
    public partial class BGM_ProjectChangeJobSite : System.Web.UI.Page
    {
        Main main = new Main();
        private ComboBLL comboBLL = new ComboBLL();
        private IProject projectBLL = new Project_BLL();
        private ManageProject manageProject = new ManageProject();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ViewState["JobSiteDataTable"] = comboBLL.ListJobsite();
                txtBgmYear.Text = SystemInfo._CurrentYearReq.ToString();
            }
        }

        protected void dtgProjectJobSite_NeedDataSource(object sender, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
        {
            int nBgmYear = (txtBgmYear.Text.Length == 4 ? Convert.ToInt32(txtBgmYear.Text) : 0);
            dtgProjectJobSite.DataSource = projectBLL.GetDataProjectByYear(nBgmYear, "");
        }

        protected void txtBgmYear_TextChanged(object sender, EventArgs e)
        {
            int nBgmYear = (txtBgmYear.Text.Length == 4 ? Convert.ToInt32(txtBgmYear.Text) : 0);
            dtgProjectJobSite.DataSource = projectBLL.GetDataProjectByYear(nBgmYear, "");
            dtgProjectJobSite.DataBind();
        }

        protected void dtgProjectJobSite_SelectedIndexChanged(object sender, EventArgs e)
        {
            foreach (GridItem item in dtgProjectJobSite.Items)
            {
                bool bSelect = item.Selected;

                Label lblJobSiteName = (Label)item.FindControl("lblJobSiteName");
                RadComboBox ddlJobSiteSeq = (RadComboBox)item.FindControl("ddlJobSiteSeq");

                lblJobSiteName.Visible = !bSelect;
                ddlJobSiteSeq.Visible = bSelect;

                if (bSelect)
                {
                    ddlJobSiteSeq.DataSource = ViewState["JobSiteDataTable"];
                    ddlJobSiteSeq.DataBind();
                }
            }
        }

        protected void dtgProjectJobSite_ItemCommand(object sender, GridCommandEventArgs e)
        {
            if (e.CommandName.ToLower() == "gridselect")
            {
                LinkButton lbnEdit = (LinkButton)e.Item.FindControl("lbnEdit");
                RadComboBox ddlJobSiteSeq = (RadComboBox)e.Item.FindControl("ddlJobSiteSeq");
                LinkButton lbnSave = (LinkButton)e.Item.FindControl("lbnSave");

                lbnEdit.Visible = false;
                ddlJobSiteSeq.Visible = true;
                lbnSave.Visible = true;

                if (ddlJobSiteSeq.Items.Count == 0)
                {
                    ddlJobSiteSeq.DataSource = ViewState["JobSiteDataTable"];
                    ddlJobSiteSeq.DataBind();
                }

                ddlJobSiteSeq.SelectedValue = main.WebString(((GridDataItem)e.Item)["job_site_seq"].Text);
            }
            else if (e.CommandName.ToLower() == "gridsave")
            {
                LinkButton lbnEdit = (LinkButton)e.Item.FindControl("lbnEdit");
                RadComboBox ddlJobSiteSeq = (RadComboBox)e.Item.FindControl("ddlJobSiteSeq");
                LinkButton lbnSave = (LinkButton)e.Item.FindControl("lbnSave");

                string strJobSiteSeq = ddlJobSiteSeq.SelectedValue;
                string strBgmProjectSeq = main.WebString(((GridDataItem)e.Item)["Sequence"].Text);

                manageProject.MoveProjectToNewJobSite(strBgmProjectSeq, strJobSiteSeq, new SsMng().EmpSeq);

                lbnEdit.Text = ddlJobSiteSeq.Text;
                lbnEdit.Visible = true;
                ddlJobSiteSeq.Visible = false;
                lbnSave.Visible = false;
            }
        }
    }
}